package com.albert.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import static com.albert.thread.ThreadPool.service;
//俩任务完成一个
public class TwoTaskEither {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("开始线程一的信息"+Thread.currentThread());
            System.out.println("输出结果1");
            return 10 / 4;
        }, service);
        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("开始线程2的信息:"+Thread.currentThread());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("输出结果2");
            return 44;
        }, service);

        //1.不感知结果,自己也没有返回值
        /*CompletableFuture<Void> future3 = future1.runAfterEitherAsync(future2, () -> {
            System.out.println("任务三执行");
        }, service);*/
        // 2.感知结果,自己不返回结果
        future1.acceptEitherAsync(future2, (res) -> {
            System.out.println("任务三执行");
        }, service);
        //3.感知结果,自己有返回值
        CompletableFuture<String> future3 = future1.applyToEitherAsync(future2, res -> {
            System.out.println("任务三执行");
            return "获取结果最终-------------";
        }, service);
        System.out.println(future3.get());
    }
}
